package com.skywolf.chem.usercenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.skywolf.chem.common.enums.ResponseCodeEnum;
import com.skywolf.chem.common.sysconfig.exception.BizException;
import com.skywolf.chem.usercenter.entity.Role;
import com.skywolf.chem.usercenter.mapper.RoleMapper;
import com.skywolf.chem.usercenter.pojo.param.RoleCreateParam;
import com.skywolf.chem.usercenter.pojo.param.RoleQueryParam;
import com.skywolf.chem.usercenter.pojo.param.RoleUpdateParam;
import com.skywolf.chem.usercenter.service.IRoleService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @author
 * @since 2019-12-02
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {

    @Override
    public Long createRole(RoleCreateParam roleCreateParam) {
        if (hasRoleName(roleCreateParam.getRoleName())) {//如果角色已存在，抛出已存在异常
            throw new BizException(ResponseCodeEnum.ROLE_DUPLICATE);
        }
        Role role = new Role(roleCreateParam.getRoleName());
        //保存角色
        this.save(role);
        return role.getId();
    }


    @Override
    public void deleteRole(Long id) {
        checkExistRole(id);
        this.removeById(id);
    }

    @Override
    public Role getRole(Long id) {
        checkExistRole(id);
        return this.getById(id);
    }


    @Override
    public void updateRole(RoleUpdateParam roleUpdateParam) {
        checkExistRole(roleUpdateParam.getId());
        Role existedRole = getByRoleName(roleUpdateParam.getRoleName());
        if (existedRole != null && !existedRole.getId().equals(roleUpdateParam.getId())) {//修改的角色名已存在
            throw new BizException(ResponseCodeEnum.ROLE_DUPLICATE);
        }
        Role role = new Role();
        BeanUtils.copyProperties(roleUpdateParam, role);
        this.updateById(role);
    }

    @Override
    public List<Role> listAllRoles() {
        return this.list();
    }

    @Override
    public boolean hasRole(Long id) {
        if (id == null) {
            return false;
        }
        return this.getById(id) != null;
    }

    @Override
    public IPage<Role> pageList(RoleQueryParam roleQueryParam) {
        return this.page(roleQueryParam.page());
    }

    /**
     * 检查是否存在角色，如果不存在则抛出角色不存在异常
     *
     * @param id
     */
    private void checkExistRole(Long id) {
        if (!hasRoleName(id)) {
            throw new BizException(ResponseCodeEnum.ROLE_NOT_FOUND);
        }
    }

    /**
     * 判断角色存不存在
     *
     * @param roleName 角色名
     * @return 存在返回 true,否则返回 false
     */
    private boolean hasRoleName(String roleName) {
        return null != this.getByRoleName(roleName);
    }

    /**
     * 判断角色存不存在
     *
     * @param id 角色id
     * @return 存在返回 true,否则返回 false
     */
    private boolean hasRoleName(Long id) {
        return null != this.getById(id);
    }

    /**
     * 根据角色名获取角色
     *
     * @param roleName
     * @return
     */
    private Role getByRoleName(String roleName) {
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("role_name", roleName);
        return this.getOne(queryWrapper);
    }


}
